package com.pybox.spec;

import com.pybox.entity.TCustomers;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TCustomers Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TCustomersSpec implements Specification<TCustomers> {

    private Map<String, String> params;

    public TCustomersSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TCustomers> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String shortName = this.params.get("shortName");
        String name = this.params.get("name");
        String country = this.params.get("country");
        String province = this.params.get("province");
        String city = this.params.get("city");
        String address = this.params.get("address");
        String landline = this.params.get("landline");
        String telephone = this.params.get("telephone");
        String phone = this.params.get("phone");
        String email = this.params.get("email");
        String state = this.params.get("state");
        String contactName = this.params.get("contactName");
        String employeeNum = this.params.get("employeeNum");
        String comments = this.params.get("comments");
        String timezone = this.params.get("timezone");
        String deleted = this.params.get("deleted");
        String ctime = this.params.get("ctime");
        String utime = this.params.get("utime");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(shortName)) {
            ps.add(criteriaBuilder.like(root.get("shortName"), "%" + shortName + "%"));
        }
        if (!StringUtils.isEmpty(name)) {
            ps.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
        }
        if (!StringUtils.isEmpty(country)) {
            ps.add(criteriaBuilder.like(root.get("country"), "%" + country + "%"));
        }
        if (!StringUtils.isEmpty(province)) {
            ps.add(criteriaBuilder.like(root.get("province"), "%" + province + "%"));
        }
        if (!StringUtils.isEmpty(city)) {
            ps.add(criteriaBuilder.like(root.get("city"), "%" + city + "%"));
        }
        if (!StringUtils.isEmpty(address)) {
            ps.add(criteriaBuilder.like(root.get("address"), "%" + address + "%"));
        }
        if (!StringUtils.isEmpty(landline)) {
            ps.add(criteriaBuilder.like(root.get("landline"), "%" + landline + "%"));
        }
        if (!StringUtils.isEmpty(telephone)) {
            ps.add(criteriaBuilder.like(root.get("telephone"), "%" + telephone + "%"));
        }
        if (!StringUtils.isEmpty(phone)) {
            ps.add(criteriaBuilder.like(root.get("phone"), "%" + phone + "%"));
        }
        if (!StringUtils.isEmpty(email)) {
            ps.add(criteriaBuilder.like(root.get("email"), "%" + email + "%"));
        }
        if (!StringUtils.isEmpty(state)) {
            ps.add(criteriaBuilder.like(root.get("state"), "%" + state + "%"));
        }
        if (!StringUtils.isEmpty(contactName)) {
            ps.add(criteriaBuilder.like(root.get("contactName"), "%" + contactName + "%"));
        }
        if (!StringUtils.isEmpty(employeeNum)) {
            ps.add(criteriaBuilder.like(root.get("employeeNum"), "%" + employeeNum + "%"));
        }
        if (!StringUtils.isEmpty(comments)) {
            ps.add(criteriaBuilder.like(root.get("comments"), "%" + comments + "%"));
        }
        if (!StringUtils.isEmpty(timezone)) {
            ps.add(criteriaBuilder.like(root.get("timezone"), "%" + timezone + "%"));
        }
        if (!StringUtils.isEmpty(deleted)) {
            ps.add(criteriaBuilder.like(root.get("deleted"), "%" + deleted + "%"));
        }
        if (!StringUtils.isEmpty(ctime)) {
            ps.add(criteriaBuilder.like(root.get("ctime"), "%" + ctime + "%"));
        }
        if (!StringUtils.isEmpty(utime)) {
            ps.add(criteriaBuilder.like(root.get("utime"), "%" + utime + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}